package com.yunkaweilai.android.activity.operation;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.classic.common.MultipleStatusView;
import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.web.WebActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.base.BaseApplication;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.AppMyCategoryModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.TitleBuilder;
import com.yunkaweilai.android.utils.Utils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 交易记录
 * Created by Administrator on 2017/8/3.
 */

public class StatisticalReportActivity extends BaseActivity {

    @BindView(R.id.content_view)
    ListView contentView;
    @BindView(R.id.id_multipleStatusView)
    MultipleStatusView idMultipleStatusView;
    private CommonAdapter<AppMyCategoryModel.DataBean.ListBean> adapter;
    private ArrayList<AppMyCategoryModel.DataBean.ListBean> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_statistical_report);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("交易记录")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        finish();
                    }
                });
        idMultipleStatusView.showLoading();
        idMultipleStatusView.setOnRetryClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initHttp();
            }
        });
        initAdapter();
        initHttp();
    }

    private void initAdapter() {
        adapter = new CommonAdapter<AppMyCategoryModel.DataBean.ListBean>(mAc,R.layout.item_list_my_item,list) {
            @Override
            protected void convert(ViewHolder viewHolder, final AppMyCategoryModel.DataBean.ListBean item, int position) {
                final String action_type = item.getAction_type();
                View idLlayoutAll = viewHolder.getView(R.id.id_llayout_all);
                viewHolder.getView(R.id.id_img_icon).setVisibility(View.GONE);
                View view = viewHolder.getView(R.id.id_views);
                view.setVisibility(View.GONE);
                idLlayoutAll.setVisibility(View.VISIBLE);
                viewHolder.setText(R.id.id_tv_name,item.getApp_category_name()+"");
                idLlayoutAll.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        switch (action_type){
                            case "AppUrl":
                                WebActivity.actionStart(mAc,item.getJump_url() + "?token=" + BaseApplication.getToken() +"&type_id=" + item.getId());
                                break;
                        }
                    }
                });

            }
        };
        contentView.setAdapter(adapter);
    }

    private void initHttp() {

        HttpRequest.postUrl(Api.APP_REPORT_CATEGORY)
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            AppMyCategoryModel appMyCategoryModel = gson.fromJson(json, AppMyCategoryModel.class);
                            list.clear();
                            list.addAll(appMyCategoryModel.getData().getList());
                            adapter.notifyDataSetChanged();
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showContent();
                            }
                        }else{
                            if (idMultipleStatusView != null) {
                                idMultipleStatusView.showError();
                            }
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        if (idMultipleStatusView != null) {
                            idMultipleStatusView.showError();
                        }
                    }
                });
    }
}
